package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.dao.CheckGroupMapper;
import com.itheima.dao.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CheckGroupMapper checkGroupMapper;

    /**
     * 添加体验套餐
     * @param checkGroupIds
     * @param setmeal
     */
    @Transactional
    public void add(String checkGroupIds, Setmeal setmeal) {
        // 添加套餐
        setmealMapper.add(setmeal);
        // 添加套餐图片到redis缓存中
        redisTemplate.opsForSet().add("valid_images",setmeal.getImg());
        // 添加套餐和检查项的关联关系
        if(checkGroupIds != null && checkGroupIds.length() > 0){
            String[] ids = checkGroupIds.split(",");
            for (String id : ids) {
                Map map = new HashMap();
                map.put("setmealId", setmeal.getId());
                map.put("checkGroupId",Integer.parseInt(id));

                setmealMapper.addSetmealAndCheckItem(map);
            }
        }

    }

    /**
     * 编辑体验套餐
     * @param checkGroupIds
     * @param setmeal
     */
    public void edit(String checkGroupIds, Setmeal setmeal) {
        // 根据id编辑套餐
        setmealMapper.edit(setmeal);
        // 根据套餐id删除套餐和检查项的关联关系
        setmealMapper.deleteSetmealAndCheckGroupBySetmealId(setmeal.getId());
        // 重新添加套餐和检查项的关联关系
        if(checkGroupIds != null && checkGroupIds.length() > 0){
            String[] ids = checkGroupIds.split(",");
            for (String id : ids) {
                Map map = new HashMap();
                map.put("setmealId", setmeal.getId());
                map.put("checkGroupId",Integer.parseInt(id));

                setmealMapper.addSetmealAndCheckItem(map);
            }
        }
    }

    /**
     * 根据套餐id查询套餐信息
     * @param id
     * @return
     */
    public Setmeal findById(Integer id) {
        // 查出部分数据
        Setmeal setmeal = setmealMapper.findById(id);

        // 查出套餐和检查项的关联关系
        List<Integer> checkGroupIds = checkGroupMapper.findCheckGroupIdsBySetmealId(id);

        if (!checkGroupIds.isEmpty() && checkGroupIds != null) {
            checkGroupIds.forEach(checkGroupId -> {
                setmeal.getCheckGroups().add(checkGroupMapper.findById(checkGroupId));
            });
        }
        return setmeal;
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> page = setmealMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 查询所有套餐信息
     * @return
     */
    public List<Setmeal> getSetmeal() {
        return setmealMapper.getSetmeal();
    }
}
